package com.tool.cn.use.search.control;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tool.cn.use.search.map.EqualContainsMap;
import com.tool.cn.use.search.map.EqualMap;
import com.tool.cn.use.search.map.IpMaskMap;
import com.tool.cn.use.search.model.DataModel;
import com.tool.cn.use.search.model.SearchInfoModel;
import com.tool.cn.use.search.search.AccurateSearch;
import com.tool.cn.use.search.search.VagueSearch;
import com.tool.cn.use.search.type.AccurateType;
import com.tool.cn.use.search.type.IpMaskType;
import com.tool.cn.use.search.type.VagueType;
import com.tool.cn.util.CollectionUtil;
import com.tool.cn.util.StringUtil;

/**
 * 
 * @author wangzonghui
 * @date 2022年4月26日 下午3:17:03 
 * @Description 日志实时过滤
 */
public class SearchControl {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	/**
	 * 精确查询对象
	 */
	private AccurateSearch accurateSearch=new AccurateSearch();
	
	/**
	 * 模糊查询对象
	 */
	private VagueSearch vagueSearch=new VagueSearch();
	
	/**
	 *
	 */
	private IpMaskMap ipMaskMap=new IpMaskMap();
	
	/**
	 * 初始化数据
	 */
	public void init(List<SearchInfoModel> dataList) {
		for(SearchInfoModel searchInfo:dataList) {
			addData(searchInfo);
		}
	}
	
	/**
	 * 获取所有value
	 */
	public Set<String> getValue(){
		Set<String> valueSet=accurateSearch.getValue();
		if(CollectionUtil.isEmptyCollection(valueSet)) {
			valueSet=new HashSet<>();
		}
		
		Set<String> tmpSet=vagueSearch.getValue();
		if(CollectionUtil.isNotEmptyCollection(tmpSet)) {
			valueSet.addAll(tmpSet);
		}
		
		tmpSet=ipMaskMap.getValue();
		if(CollectionUtil.isNotEmptyCollection(tmpSet)) {
			valueSet.addAll(tmpSet);
		}
		return valueSet;
	}
	
	public void printData() {
		String tmp;
		log.info("----Accruate----");
		for(Entry<AccurateType,EqualMap> data:accurateSearch.getData().entrySet()) {
			tmp=data.getValue().toString();
			if(StringUtil.isNotEmpty(tmp)) {
				log.info(data.getKey().name()+"-"+tmp);
			}
				
		}
		
		log.info("----Vauge----");
		for(Entry<VagueType, EqualContainsMap> data:vagueSearch.getData().entrySet()) {
			tmp=data.getValue().toString();
			if(StringUtil.isNotEmpty(tmp)) {
				log.info(data.getKey().name()+"-"+tmp);
			}
		}
		
		log.info("----sourceIpObjectIp----");
		tmp=ipMaskMap.toString();
		if(StringUtil.isNotEmpty(tmp)) {
			log.info(ipMaskMap.toString());
		}
	}
	/**
	 * 添加数据
	 * @param searchInfo
	 */
	public void addData(SearchInfoModel searchInfo) {
		String data="";
		
		if(searchInfo.getSearchType()<VagueType.phoneSourceIpMask.getValue()) {  //精确查询
			initAccurate(searchInfo,data);
			
		}else if (searchInfo.getSearchType() <IpMaskType.twoIpMask.getValue()) { //精确加模糊查询
			initVague(searchInfo,data);
			
		}else if(searchInfo.getSearchType()==IpMaskType.twoIpMask.getValue()) {  //双ip+掩码查询
			if(searchInfo.getSearchType()==IpMaskType.twoIpMask.getValue()) {
				//ipMaskMap.add(key, data);
			}else {
				log.error("No Found This Type :{} Of TwoIpMask Search,All Info:{}",searchInfo.getSearchType(),searchInfo.toString());
			}
		}else {
			log.error("No Found This Type :{} Info:{}",searchInfo.getSearchType(),searchInfo.toString());
		}
	}
	
	/**
	 * 初始化模糊查询
	 * @param searchInfo
	 * @param data
	 */
	private void initVague(SearchInfoModel searchInfo,String data) {
		if(StringUtil.isEmpty(data)) {  
			//TODO 添加数据
			//vagueSearch.addData(type, equal, ipMask, data);
		}else{
			log.error("No Found This Of Vague Search,All Info:{}",searchInfo.toString());
		}
	}
	
	/**
	 * 初始化精确查询数据
	 * @param searchInfo
	 * @param data
	 */
	private void initAccurate(SearchInfoModel searchInfo,String data) {
		if(StringUtil.isEmpty(data)) {  
			//TODO 添加数据
			//accurateSearch.addData(type, key, data);
		}else {
			log.error("No Found This  Of Accurate Search,All Info:{}",searchInfo.toString());
		}
		
	}
	
	/**
	 * 数据查询
	 * @param dataModel
	 * @return
	 */
	public List<String> search(DataModel dataModel){
		
		List<String> dataList=new ArrayList<>();
		List<String> tmpList;
		if(StringUtil.isNotEmpty(dataModel.getPhone())){
			String phone=dataModel.getPhone();
			//手机号
			tmpList=accurateSearch.search(AccurateType.phone, phone);
			dataList=CollectionUtil.addAll(dataList, tmpList);
			
			//手机号+源公网ip
			if(StringUtil.isNotEmpty(dataModel.getSourceIp())) {
				tmpList=accurateSearch.search(AccurateType.phoneSourceIp, phone+StringUtil.line+dataModel.getSourceIp());
				dataList=CollectionUtil.addAll(dataList, tmpList);
				
				//手机号+源公网ip+掩码
				tmpList=vagueSearch.search(VagueType.phoneSourceIpMask,phone,dataModel.getSourceIp());
				dataList=CollectionUtil.addAll(dataList, tmpList);
			}
		}
		
		return dataList;
	}

	/**
	 * 添加精确查询数据
	 * @param type
	 * @param key
	 * @param data
	 * @return
	 */
	public boolean addAccurate(AccurateType type,String key,String data) {
		return accurateSearch.addData(type, key, data);
	}
	
	/**
	 * 删除精确查询数据
	 * @param type
	 * @param key
	 * @param data
	 * @return
	 */
	public boolean removeAccurate(AccurateType type,String key,String data) {
		return accurateSearch.removeData(type, key, data);
	}
	
	/**
	 * 添加模糊查询数据
	 * @return
	 */
	public boolean addVague(VagueType type,String equal,String ipMask,String data) {
		return vagueSearch.addData(type, equal, ipMask, data);
	}

	/**
	 * 删除模糊查询数据
	 * @return
	 */
	public boolean removeVague(VagueType type,String equal,String ipMask,String data) {
		return vagueSearch.removeData(type, equal, ipMask, data);
	}
	
	
	/**
	 * 添加源公网ip+公网掩码+目的ip+目的掩码
	 * @return
	 */
	public boolean addIpMask(String sourceIpMask,String objectiveIpMask,String data) {
		return ipMaskMap.add(sourceIpMask, objectiveIpMask, data);
	}

	/**
	 * 删除源公网ip+公网掩码+目的ip+目的掩码
	 * @return
	 */
	public boolean removeIpMask(String sourceIpMask,String objectiveIpMask,String data) {
		
		return ipMaskMap.remove(sourceIpMask, objectiveIpMask, data);
	}
	
	/**
	 * 删除查询信息
	 * @param data
	 */
	public void removeData(String data) {
		accurateSearch.removeData(data);
		vagueSearch.removeData(data);
		ipMaskMap.remove(data);
	}
	
}
